home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / AMUG Info / Apple / MacsBug 6.2.2 / dcmds / C Samples / Printf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-19  |  2.7 KB  |  105 lines  |  [TEXT/MPS ]

  1. /*     printf.c
  2.     This is the printf dcmd.  This is mostly to show how dcmds can use the standard
  3.     C library.
  4.  
  5.     Copyright © 1989 Apple Computer, Inc.  All rights reserved.
  6.  
  7.     Modification history:
  8.         16Feb89 sad        written
  9.  
  10.     The following MPW commands will build the dcmd and copy it to the
  11.     "Debugger Prefs" file in the System folder. The dcmd's name in
  12.     MacsBug will be the name of the file built by the Linker.
  13.     You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  14.     C Samples folder into this folder.
  15.  
  16.     C Put.c
  17.     C Printf.c
  18.     Link dcmdGlue.a.o Printf.c.o put.c.o -d DRuntime.o "{Libraries}"Interface.o ∂
  19.          "{CLibraries}StdCLib.o" "{CLibraries}CInterface.o" -sg Main=STDIO -o Printf
  20.     BuildDcmd Printf 1004
  21.     Echo 'include "Printf";'    |    Rez -a -o "{systemFolder}Debugger Prefs"
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. #include <Types.h>
  28.  
  29. #include "dcmd.h"
  30. #include "put.h"
  31.  
  32. pascal void CommandEntry(dcmdBlock* paramPtr)
  33. {
  34.     switch (paramPtr->request)
  35.         {
  36.         case dcmdInit:
  37.             break;
  38.  
  39.         case dcmdHelp:
  40.             dcmdDrawLine("\pprintf \"format\" arg...");
  41.             dcmdDrawLine("\p   Displays the arguments according to the format (no floating point).");
  42.             break;
  43.  
  44.         case dcmdDoIt:
  45.             {
  46.             Str255 formatstring;
  47.             unsigned char* formatp = formatstring;
  48.             Str255 formattedstring;
  49.  
  50.             dcmdSwapWorlds();            // not really necessary because we don’t access machine state
  51.  
  52.             (void)dcmdGetNextParameter(formatstring);
  53.  
  54.             p2cstr(formatstring);
  55.  
  56.             while (*formatp != '\0')
  57.                 {
  58.                 char aformatspec[256];
  59.                 size_t speclength;
  60.                 long arg;
  61.                 Boolean ok;
  62.  
  63.                 // find the next format spec (i.e. %d) and print out anything before it
  64.                 char* nextp = strchr(formatp, '%');
  65.                 if (nextp == nil) nextp = formatp + strlen(formatp);
  66.  
  67.                 PutBytesTo(formatp, nextp - formatp, 0);
  68.                 formatp = nextp;
  69.  
  70.                 // find the length of the format spec
  71.                 speclength = strcspn(formatp + 1, "diouxXcspP%") + 2;    // we don’t do "feEgGn"
  72.                 memcpy(aformatspec, formatp, speclength);
  73.                 aformatspec[speclength] = '\0';
  74.                 (void)dcmdGetNextExpression(&arg, &ok);
  75.                 if (!ok) break;
  76.  
  77.                 sprintf(formattedstring, aformatspec, arg);
  78.                 PutCStr(formattedstring);
  79.                 formatp = formatp + speclength;
  80.                 }
  81.             PutLine();
  82.  
  83.             dcmdSwapWorlds();
  84.             }
  85.             break;
  86.  
  87.         default:
  88.             PutPStr("\punknown request ");
  89.             PutUDec(paramPtr->request);
  90.             PutLine();
  91.             break;
  92.         }
  93. } // CommandEntry
  94.  
  95. /*
  96.     the following are stubs to override the C library routines so that the dcmd isn’t
  97.     so big.
  98. */
  99.  
  100. size_t fwrite (const void *, size_t, size_t, FILE *) { return 0; }        // wont’t actually be called by sprintf
  101. _flsbuf() {}    // wont’t actually be called by sprintf
  102.  
  103. fcvt() {}        // used only for floating point %f, etc.
  104. ecvt() {}        // used only for floating point %e, etc.
  105.